/****** Object:  StoredProcedure [dbo].[sp_po_futures_order_invert]    Script Date: 11/11/2009 13:20:22 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_po_futures_order_invert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_po_futures_order_invert]
GO
/****** Object:  StoredProcedure [dbo].[sp_po_futures_order_invert]    Script Date: 11/11/2009 13:18:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/***********************************************************************
* sp Name: dbo.sp_po_futures_order_invert
* Created: 20100804, RGM
* Purpose: invierte el valor de futures_order para dos PO dadas
*
* Modified: yyyymmdd user, data 
*
* HistoryDate.
* yyyymmdd User, data
* 
* Note: nothing
* 
***********************************************************************/
CREATE PROCEDURE [dbo].[sp_po_futures_order_invert]
	@purchaseOrderVersionId1	INT, 
	@purchaseOrderVersionId2	INT, 
	@DEBUG						INT = 0
AS

/*
	error handling
*/
BEGIN TRY
	DECLARE
		@TranStarted BIT
	SET @TranStarted = 0

	IF( @@TRANCOUNT = 0 ) BEGIN
		BEGIN TRANSACTION
		SET @TranStarted = 1
	END ELSE BEGIN
		SET @TranStarted = 0
	END

	SET NOCOUNT ON
-------------------------------------------------------
--[BEGIN] SP section
DECLARE @text VARCHAR(512)

SET @text = '	------ Ejecutando sp_po_futures_order_invert ' 
		+ CONVERT(VARCHAR(10), @purchaseOrderVersionId1) + ', ' + 
		+ CONVERT(VARCHAR(10), @purchaseOrderVersionId2)

IF @DEBUG = 1
	PRINT @text

DECLARE 
	@bridge		INT, 

-- manejo de entorno
	@RCOUNT					INT

-- se graba el primer valor del orden
SELECT @bridge = futures_order
	FROM purchases_orders_versions
	WHERE purchase_order_version_id = @purchaseOrderVersionId1

-- se actualiza el orden del 1 con el orden del 2
UPDATE pov1 SET
		pov1.futures_order = pov2.futures_order
	FROM purchases_orders_versions pov1, purchases_orders_versions pov2
	WHERE pov1.purchase_order_version_id = @purchaseOrderVersionId1
		AND pov2.purchase_order_version_id = @purchaseOrderVersionId2

SET @RCOUNT = @@ROWCOUNT
IF @DEBUG = 1
	PRINT 'U:purchases_orders_versions [' + CONVERT(VARCHAR(10), @RCOUNT) + ']'

-- se actualiza el orden del 2 con el @bridge
UPDATE purchases_orders_versions SET
		futures_order = @bridge
	WHERE purchase_order_version_id = @purchaseOrderVersionId2

SET @RCOUNT = @@ROWCOUNT
IF @DEBUG = 1
	PRINT 'U:purchases_orders_versions [' + CONVERT(VARCHAR(10), @RCOUNT) + ']'

IF @DEBUG = 1
	PRINT 'END:' + @text

--[END] SP section
-------------------------------------------------------
	SET NOCOUNT OFF

	IF( @TranStarted = 1 ) BEGIN
		SET @TranStarted = 0
		COMMIT TRANSACTION
	END

	RETURN 1 --simil boleano a true
END TRY BEGIN CATCH
	DECLARE 
		@ErrorMessage	NVARCHAR(4000), 
		@ErrorNumber	INT, 
		@ErrorSeverity	INT, 
		@ErrorState		INT

	SELECT @ErrorMessage = ERROR_MESSAGE(),
				@ErrorSeverity = ERROR_SEVERITY(),
				@ErrorState = ERROR_STATE(),
				@ErrorNumber = ERROR_NUMBER()

	IF( @TranStarted = 1 ) BEGIN
		SET @TranStarted = 0
		ROLLBACK TRANSACTION
	END

	RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)
	RETURN 0 --simil boleano a mal
END CATCH 



